﻿using AutoMapper;
using MediatR;
using Return.Enum;
using SlaughterItem.Common.Infrastructures.Interfaces;
using THE_WIND_RISES.Common.Domans.Produce.Procurement;
using THE_WIND_RISES.Common.Domans.Produce.Supplier;
using THE_WIND_RISES.Common.Dtos;
namespace Production_Set.API.Application.Commands.Procurement
{
    public class OrderLinesUpdateHandler : IRequestHandler<OrderLinesUpdateCommand, Result>
    {
        /// <summary>
        /// 采购/委外订单头信息
        /// </summary>
        private readonly IBaseRepository<OrderLines> OrderLinesRepository;
        /// <summary>
        /// 映射
        /// </summary>
        private readonly IMapper mapper;
        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<OrderLinesUpdateHandler> logger;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="purchasingsRepository"></param>
        /// <param name="mapper"></param>
        /// <param name="logger"></param>
        public OrderLinesUpdateHandler(IBaseRepository<OrderLines> OrderLinesRepository, IMapper mapper,  ILogger<OrderLinesUpdateHandler> logger)
        {
            this.OrderLinesRepository = OrderLinesRepository;
            this.mapper = mapper;
            this.logger = logger;
        }
        public async Task<Result> Handle(OrderLinesUpdateCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCodes.Update };

            try
            {
                var AddData = mapper.Map<OrderLines>(request);
                string sql = "UPDATE OrderLines SET InventoryInfoId=@InventoryInfoId ,OrderLinePrice=@OrderLinePrice ,OrderLineNum=@OrderLineNum ,Unit=@Unit,BankAmount=@BankAmount ,LineSummary=@LineSummary WHERE OrderLineId =@OrderLineId ";
                var AddNum = OrderLinesRepository.Update(sql, new
                {
                    OrderLineId = AddData.OrderLineId,
                    InventoryInfoId = AddData.InventoryInfoId,
                    OrderLinePrice = AddData.OrderLinePrice,
                    OrderLineNum = AddData.OrderLineNum,
                    Unit = AddData.Unit,
                    BankAmount = AddData.BankAmount,
                    LineSummary = AddData.LineSummary,
                });
                if (AddNum > 0)
                {
                    result.Code = (int)CommonErrorCodes.Success;
                    result.Data = null;
                    result.Msg = "订单行修改成功";
                    logger.LogInformation("订单行修改成功");
                }
                else
                {
                    result.Code = (int)CommonErrorCodes.Update;
                    result.Data = null;
                    result.Msg = "订单行修改失败";
                    logger.LogError("订单行修改失败，");

                }
            }
            catch (Exception ex)
            {
                logger.LogError($"订单行修改失败" + ex.Message);
                throw;
            }
            return result;
        }
    }
}
